package com.tjtt.gwm.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.gwm.bean.Junyansyq;
import com.tjtt.gwm.bean.TdmEquipment;
import com.tjtt.gwm.service.EquipmentService;
import com.tjtt.gwm.service.JunyansService;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.codeReview.service.ICodeReviewService;
import com.tjtt.tdm.util.PubMethod;

public class JunyansYiqiAction extends BaseAction {
	private EquipmentService epService;
	private JunyansService junyansService;
	private ICodeReviewService codeReviewService;
	public EquipmentService getEpService() {
		return epService;
	}

	public void setEpService(EquipmentService epService) {
		this.epService = epService;
	}

	public JunyansService getJunyansService() {
		return junyansService;
	}

	public void setJunyansService(JunyansService junyansService) {
		this.junyansService = junyansService;
	}

	public ICodeReviewService getCodeReviewService() {
		return codeReviewService;
	}

	public void setCodeReviewService(ICodeReviewService codeReviewService) {
		this.codeReviewService = codeReviewService;
	}

	/**
	 * 选择页面仪器
	 */
	public void querySelectYiqi(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			String type = getRequest().getParameter("type");
			String yiqimc = getRequest().getParameter("yiqimc");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			if(null!=type && !"".equals(type)){
				filterMap.put("type", StringUtil.encodingUrl(type));
			}
			if(null!=yiqimc && !"".equals(yiqimc)){
				filterMap.put("yiqimc", StringUtil.encodingUrl(yiqimc));
			}
			PageResult result = null;
			if (type.equals("all")){
				result= junyansService.querySelectYiqiAll(filterMap);
			}
			else{
				result= junyansService.querySelectYiqiSelect(filterMap);
			}
			String[] viewArray={"ID","YIQILX","YIQIMC","GUIGEXH","ZHIZAOCS"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void addYiqiTo(){
		try{
			String yiqiids = getRequest().getParameter("yiqiids");
			String junyansid = getRequest().getParameter("junyansid");
			String[] yiqiidarray = yiqiids.split(",");
			
			for(int i=0;i<yiqiidarray.length;i++){
				Junyansyq entity = new Junyansyq();
				entity.setJunyansid(Long.parseLong(junyansid));
				entity.setYiqiid(Long.parseLong(yiqiidarray[i]));
				epService.saveObject(entity);
				
				TdmEquipment yiqi = (TdmEquipment)epService.getDataById(TdmEquipment.class, Long.parseLong(yiqiidarray[i]));
				yiqi.setShiyongzt("试验中");
				epService.updateObject(yiqi);
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void delYiqiTo(){
		try{
			String yiqiids = getRequest().getParameter("yiqiids");
			String junyansid = getRequest().getParameter("junyansid");
			String[] yiqiidarray = yiqiids.split(",");
			for(int i=0;i<yiqiidarray.length;i++){
				String strSql = "delete from kl_junyans_yq where junyansid='"+junyansid+"' and yiqiid='"+yiqiidarray[i]+"'";
				epService.exeSql(strSql);
				
				TdmEquipment yiqi = (TdmEquipment)epService.getDataById(TdmEquipment.class, Long.parseLong(yiqiidarray[i]));
				yiqi.setShiyongzt("维护");
				epService.updateObject(yiqi);
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}

}
